﻿#region File Description
//-----------------------------------------------------------------------------
// Enemy.cs
//
// Attack of the Crazy Pixels
// Copyright (c) 2009 VosSoft
//-----------------------------------------------------------------------------
#endregion
#region License
//-----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2009 VosSoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Attack_of_the_Crazy_Pixels
{
    /// <summary>
    /// Defines an Enemy type.
    /// </summary>
    public class Enemy : Sprite
    {
        #region Fields

        public static Texture2D EnemyTexture;

        protected Level level;

        protected float speed;
        protected readonly float hitPoints;
        protected float currentHitPoints;

        protected int points;

        int pointHeading;
        Vector2 direction;
        float distance, currentDistance;

        #endregion

        #region Properties

        public float HitPoints
        {
            get { return currentHitPoints; }
        }

        public float HitPointsPercentage
        {
            get { return currentHitPoints / hitPoints; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Creates a new Enemy.
        /// </summary>
        /// <param name="speed">Speed of the Enemy (pixels per second).</param>
        /// <param name="hitpoints">The hitpoints of the Enemy.</param>
        /// <param name="color">The color of the Enemy.</param>
        /// <param name="points">The points the Player gets if the Enemy is killed.</param>
        /// <param name="level">Backreference to the Level.</param>
        public Enemy(float speed, float hitpoints, Color color, int points, Level level)
            : base(EnemyTexture, level.Path.Points[0], color)
        {
            this.speed = speed;
            this.hitPoints = hitpoints;
            this.currentHitPoints = hitpoints;
            this.points = points;
            this.level = level;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Moves the Enemy with the defined speed forwand along the Level path.
        /// </summary>
        /// <param name="elapsedTime">Time since the last frame (in seconds).</param>
        public void Move(float elapsedTime)
        {
            float realSpeed = speed * elapsedTime;
            currentDistance += realSpeed;
            if (currentDistance >= distance)
            {
                // end of the current vector in the path is reached
                pointHeading++;
                if (pointHeading >= level.Path.Count) // end of path reached
                {
                    // position the enemy out of the visible screen, this ist just for Towers
                    // in range of the last level path point, so that they have to search a new
                    // target and and get rid of the remaining reference to this enemy.
                    // (Otherwise it won't be delted until the enemy is dead!)
                    Position = new Vector2(-1000.0f);

                    level.EnemiesDestinationReached++;
                    level.Enemies.Remove(this);

                    level.Player.Lives--;

                    return;
                }
                direction = level.Path[pointHeading] - level.Path[pointHeading - 1];
                distance = direction.Length();
                direction.Normalize();
                Rotation = Calc.VectorToRadians(direction);
                currentDistance = 0.0f;
            }
            Position += direction * realSpeed;
        }

        /// <summary>
        /// Hit the Enemy with a Projectile.
        /// </summary>
        /// <param name="damage">The damage the Projectile makes.</param>
        /// <returns>Returns true if the Enemy is dead.</returns>
        public bool Hit(float damage)
        {
            currentHitPoints -= damage;
            level.Player.Points++;
            if (currentHitPoints <= 0) // with no hitpoints left the Enemy is dead ;)
            {
                level.EnemiesKilled++;
                level.Player.Money += points;
                level.Player.Points += points;
                level.Enemies.Remove(this);
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
